package com.dyj.ads

import org.apache.spark.sql.expressions.Window
import org.apache.spark.sql.{DataFrame, SparkSession}
object ads_e_mz_ave763b_hxb {
  def main(args: Array[String]): Unit = {
    val ds: String = args(0)

    val sparkSession: SparkSession = SparkSession.builder()
      .appName("红细胞指标统计")
      .enableHiveSupport()
      .config("spark.sql.shuffle.partitions", 1)
      .getOrCreate()

    import sparkSession.implicits._
    import org.apache.spark.sql.functions._

    sparkSession.sql("use bigdata03_dws")

    val hxbDF: DataFrame = sparkSession.sql(
      s"""
        |select
        |baoGaoBianHao,
        |hongxibao,
        |baoGaoRiQi,
        |hxb_max,
        |hxb_min,
        |hxb_avg
        |from
        |bigdata03_dws.dws_e_mz_ave763bdabianniaochanggui
        |where
        |ds='${ds}'
        |""".stripMargin)

    hxbDF
      .withColumn("number", count($"baoGaoBianHao") over Window.partitionBy($"baoGaoBianHao"))
      .withColumn("e_fc", ($"hongxibao" - $"hxb_avg") * ($"hongxibao" - $"hxb_avg"))
      .withColumn("a_fc", sum($"e_fc").over())
      .withColumn("hxb_fc", $"a_fc" / $"number")
      .withColumn("hxb_bzc", sqrt($"hxb_fc"))
      .withColumn("hxb_ycgs", sum(when($"hongxibao" > 10000 or ($"hongxibao" < 4000), 1)).over())
      .select($"baoGaoBianHao", $"hongxibao", $"baoGaoRiQi", $"hxb_max", $"hxb_min", $"hxb_avg", $"hxb_fc", $"hxb_bzc", $"hxb_ycgs")
      .write
      .format("csv")
      .save("/daas/motl/bigdata03/ads/ads_e_mz_ave763b_hxb/ds=" + ds)
  }
}
